<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script type="text/javascript">
        // 0 1 1 2 3 5 8 13 21 34 ...
        // 迭代算法

        function fib(n) {
            if (n <= 1) return n;
            let t;
            let res1 = 0;
            let res2 = 1;
            for (let i = 1; i < n; i++) { //一定要注意循环从1开始
                t = res1;
                res1 = res2;
                res2 = t + res2;
            }
            return res2
        }
        console.log(fib(6));


        // 递归算法
        // function recursion(n) {
        //     if (n <= 1) return n;
        //     return recursion(n - 1) + recursion(n - 2);
        // }
        // console.log(recursion(6))
    </script>
</body>

</html>